package br.com.werp.suporte.Acesso;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 20/02/2006 09:56:43
 * Last Modify Date 27/04/2006 16:49:11
 */

public class UrlAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_insert_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      insert_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void insert_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    UrlActionForm f = (UrlActionForm) form;
    br.com.werp.suporte.dbobj.table.Url table =
      new br.com.werp.suporte.dbobj.table.Url();
    Object[][] name_value = { {"Url_path",f.getUrl_path()}, };
    //###############################auditoria.INCLUIR#########################
    try {
      j.setAutoCommit(false);
      j.insert(table, name_value);
      br.com.werp.regras.geral.Auditoria auditoria =
        br.com.werp.regras.geral.FabricaSubSistema.getAuditoria();
      String usuario = (String) session.getAttribute("j_username");
      auditoria.auditarRecurso(j.getConn(), usuario, table,
          auditoria.INCLUIR);
      j.commit();
    } catch (Exception e) {
      j.rollback();
      throw e;
    }
    finally{ j.setAutoCommit(true);
           } //###############################auditoria.INCLUIR#########################
    session.setAttribute("UrlActionForm",new UrlActionForm());
    Vector res_Url = (Vector) session.getAttribute("res_Url");
    if (res_Url != null && table != null) {
      res_Url.addElement(table);
      session.setAttribute("res_Url", res_Url);
    }
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.removeAttribute("res_Url");
      form.reset(mapping, request);
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

}
